package com.pure.common;

import java.beans.PropertyDescriptor;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.springframework.web.multipart.MultipartFile;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Util {
	public static final String spatialReference = "4490";

	public static Document readXml() {
		try {
			// Document doc = DocumentHelper.parseText("");
			SAXReader saxReader = new SAXReader();
			File configFile = new File("/WEB-INFclassesServiceCfg.xml");
			Document document = saxReader.read(configFile);
			return document;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 在list中添加map，如果mapKeyName值与mapKeyValue相同,就替换该元素,否则就新加入一个元素
	 * 
	 * @param list
	 * @param mapKeyName
	 *            用来判断是否相同的字段名
	 * @param mapKeyValue
	 *            用来判断是否相同的字段名的值
	 * @return
	 */
	public static void addMapToListByMapKey(List<Map<String, String>> list, String mapKeyName,
			Map<String, String> cmap) {
		if (list == null || list.size() == 0 || mapKeyName == null || cmap == null) {
			return;
		}
		boolean isAdd = false;
		// 循环查找是否已经有该元素,如果有就替换
		for (int i = 0; i < list.size(); i++) {
			Map<String, String> map = list.get(i);
			if (cmap.get(mapKeyName).equals(map.get(mapKeyName))) {// 获取替换的map
				list.remove(i);
				list.add(i, cmap);
				isAdd = true;
				break;
			}
		}
		// 如果没有该元素,就新加入
		if (!isAdd) {
			list.add(cmap);
		}
	}

	/***
	 * 根据指定的数字字符串,获取自增1后,指定位数lenth长度的字符串(位数不够的用0补充)
	 * 如果自增后的数字大于需要返回的位数,则返回自增后的数字字符窜
	 * 
	 * @param numStr
	 *            数字字符串
	 * @param lenth
	 *            需要的返回的字符串长度
	 * @return
	 */
	public static String getStrForNumStr(String numStr, int lenth) {
		if (numStr == null || numStr.equals("")) {
			return "";
		}
		Integer num = Integer.parseInt(numStr) + 1;
		String str = num + "";
		if (str.length() >= lenth) {// 如果数字长度大于等于需要返回的长度,则直接返回该数字
			return str;
		} else {// 如果小于需要返回的长度,则用零来补充
			for (int i = str.length(); i < lenth; i++) {
				str = "0" + str;
			}
		}
		return str;
	}

	

	public static float string2Float(String data) {
		try {
			return Float.valueOf(data);
		} catch (Exception e) {
			return 0;
		}
	}

	/***
	 * 解析匹配具体查询条件
	 * 
	 * @param dataType
	 *            字段数据类型
	 * @param operationDesc
	 *            条件运算描述
	 * @param column
	 *            数据库匹配字段名
	 * @param value
	 *            条件值
	 * @return
	 */
	public static String splitCondtion(String dataType, String operationDesc, String column, String value) {
		String str = "";
		String operation = getOperationCondition(operationDesc);
		if ("String".equalsIgnoreCase(dataType)) {// 字符窜类型
			if (operationDesc.contains("包含")) {// 包含或不包含
				str = " " + column + " " + operation + " '%" + value + "%'";
			} else {
				str = " " + column + " " + operation + " '" + value + "'";
			}
		} else if ("Date".equalsIgnoreCase(dataType)) {// 日期类型

		} else if ("Number".equalsIgnoreCase(dataType)) {// 数字窜类型
			str = " " + column + " " + operation + " " + value;
		}
		return str;
	}

	/**
	 * 根据运算符描述获取运算符
	 * 
	 * @param operationDesc
	 *            运算符描述
	 * @return
	 */
	public static String getOperationCondition(String operationDesc) {
		String operation = "";
		if ("大于".equalsIgnoreCase(operationDesc)) {
			operation = ">";
		} else if ("大于等于".equalsIgnoreCase(operationDesc)) {
			operation = ">=";
		} else if ("小于".equalsIgnoreCase(operationDesc)) {
			operation = "<";
		} else if ("小于等于".equalsIgnoreCase(operationDesc)) {
			operation = "<=";
		} else if ("等于".equalsIgnoreCase(operationDesc)) {
			operation = "=";
		} else if ("不等于".equalsIgnoreCase(operationDesc)) {
			operation = "!=";
		} else if ("包含".equalsIgnoreCase(operationDesc)) {
			operation = "like";
		} else if ("不包含".equalsIgnoreCase(operationDesc)) {
			operation = "not like";
		}
		return operation;
	}

	/**
	 * @param type
	 *            0 获取原始的uuid,1 获取不带中间-的32位的纯字符的uuid
	 * @return
	 */
	public static String getUuid(String type) {
		String uuid = UUID.randomUUID().toString();
		if ("1".equals(type)) {
			uuid = uuid.replace("-", "");
		}
		return uuid;
	}

	/***
	 * 获取数组中,n个不重复的随机元素
	 * 
	 * @param obj
	 * @param n
	 * @return
	 */
	public static String[] randomArray(String[] obj, int n) {
		if (obj == null || obj.length == 0) {
			return null;
		}
		if (n >= obj.length) {
			return obj;
		}
		List<String> list = new LinkedList<String>();
		Collections.addAll(list, obj);
		Random r = new Random();
		String[] strarray = new String[n];
		int index = 0;
		for (int i = 0; i < n; i++) {
			// 随着循环的继续,逐渐抛弃后面的元素
			index = r.nextInt(list.size());
			strarray[i] = list.get(index);
			// 删除list中的该元素
			list.remove(index);
		}
		return strarray;
	}

	public static void main(String[] args) {
		String[] data = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
				"18", "19", "20" };
		data = randomArray(data, 19);
		for (String str : data) {
			System.out.println(str);
		}
	}

	/**
	 * 字符串是否为空串，包括本身字符串为null以及""
	 * 
	 * @param target
	 *            目标串
	 * @return 目标串是否为空串
	 * @author huangzhen
	 */
	public static boolean isNullString(String target) {
		if (target == null || "".equals(target.trim())) {
			return true;
		}
		return false;
	}

	/**
	 * Date转换为字符串
	 * 
	 * @param date
	 * @return
	 * @author Huangzhen
	 */
	public static String dateFormat(Date date, String pattern) {
		if (date == null || isNullString(pattern)) {
			return "";
		}
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}

	/**
	 * 保存上传的文件
	 * 
	 * @param file
	 * @param path
	 * @return
	 */
	public static String saveMultipartFileEnc(MultipartFile file, String path) {
		if (null != file && !file.isEmpty()) {
			try {
				File upload = new File(path);
				if (!upload.isDirectory()) {
					upload.mkdirs();
				}
				String name = file.getOriginalFilename();
				String type = name.substring(name.lastIndexOf("."));
				name = Util.getUuid("1") + type + ".enc";
				String filePath = path + File.separator + name;
				File fFile = new File(filePath);
				file.transferTo(fFile);
				return name;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "";
	}

	/**
	 * 保存上传的文件
	 * 
	 * @param file
	 * @param path
	 * @return
	 */
	public static String saveMultipartFile(MultipartFile file, String path) {
		if (null != file && !file.isEmpty()) {
			try {
				File upload = new File(path);
				if (!upload.isDirectory()) {
					upload.mkdirs();
				}
				String name = file.getOriginalFilename();
				File fFile = new File(path + File.separator + name);
				file.transferTo(fFile);
				return name;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "";
	}

	/**
	 * 将javabean转为map类型，然后返回一个map类型的值
	 * 
	 * @param obj
	 * @return
	 * @author wt
	 */
	public static Map<String, Object> beanToMap(Object obj) {
		Map<String, Object> params = new HashMap<String, Object>(0);
		try {
			PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
			PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj);
			for (int i = 0; i < descriptors.length; i++) {
				String name = descriptors[i].getName();
				if (!StringUtils.equals(name, "class")) {
					params.put(name, propertyUtilsBean.getNestedProperty(obj, name));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return params;
	}

	/**
	 * Json转Map
	 * 
	 * @param jsonStr
	 * @return
	 * @author wt
	 */
	public static Map<String, Object> jsonToMap(String jsonStr) {
		JSONObject json = JSONObject.fromObject(jsonStr);
		return jsonToMap(json);
	}

	public static Map<String, Object> jsonToMap(JSONObject json) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 最外层解析
		for (Object k : json.keySet()) {
			Object v = json.get(k);
			// 如果内层还是数组的话，继续解析
			if (v instanceof JSONArray) {
				List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
				Iterator<JSONObject> it = ((JSONArray) v).iterator();
				while (it.hasNext()) {
					JSONObject json2 = it.next();
					list.add(jsonToMap(json2.toString()));
				}
				map.put(k.toString(), list);
			} else {
				map.put(k.toString(), v);
			}
		}
		return map;
	}

	/**
	 * 统计分析 解析审核状态
	 */
	public static List<Map<String, Object>> analyStatus(List<Map<String, Object>> list) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		Map<String, Object> tempMap = new HashMap<String, Object>();
		tempMap.put("status", "提交");
		tempMap.put("num", 0);
		int i = 0;
		for (Map<String, Object> map : list) {

			if (map.get("status") == "" || map.get("status") == null) {
				continue;
			}
			if ("1".equals(map.get("status").toString())) {
				map.put("status", "审核通过");
				i += Integer.parseInt(map.get("num").toString());
			}
			// if ("-1".equals(map.get("status").toString())) {
			// map.put("status", "未提交");
			// }
			if ("-2".equals(map.get("status").toString())) {
				map.put("status", "退回");
				i += Integer.parseInt(map.get("num").toString());
			}
		}
		tempMap.put("num", i);
		list.add(tempMap);
		return list;
	}

	/**
	 * 统计查询结果，
	 * 
	 * @param tempResult
	 *            查询结果
	 * @param i
	 *            1表示区/市级部门 2 表示市政府 3表示乡镇
	 * @return
	 */
	public static List<List<Map<String, Object>>> statisticArea(List<Map<String, Object>> tempResult, int i) {
		// TODO Auto-generated method stub
		List<List<Map<String, Object>>> result = new ArrayList();
		// 子集
		List<Map<String, Object>> ListResult = new ArrayList<Map<String, Object>>();

		if (i == 1) {
			// 管辖区下乡镇行政编码
			Set<Map<String, Object>> area = new HashSet<Map<String, Object>>();
			// 解析乡镇
			for (Map map : tempResult) {
				Map<String, Object> temmap = new HashMap<String, Object>();
				temmap.put("fregionCode", (map.get("fregionCode")));
				temmap.put("fregionName", map.get("fregionName"));
				area.add(temmap);
			}

			// 统计乡镇下 所有 村社的审核状态
			for (Map<String, Object> templ : area) {
				Map<String, Object> map = new HashMap<String, Object>();
				// 状态和数量
				List<Map<String, Object>> status = new ArrayList<Map<String, Object>>();
				map.put("fregionName", templ.get("fregionName"));
				Map<String, Object> backMap = new HashMap<String, Object>();
				Map<String, Object> passMap = new HashMap<String, Object>();
				// Map<String, Object> tempMap = new HashMap<String, Object>();
				// Map<String, Object> notCommitMap = new HashMap<String,
				// Object>();
				Map<String, Object> commitMap = new HashMap<String, Object>();
				backMap.put("num", 0);
				passMap.put("num", 0);
				// tempMap.put("num", 0);
				// notCommitMap.put("num", 0);
				// passMap.put("status", "未处理");
				passMap.put("status", "通过");
				// notCommitMap.put("status", "未提交");
				backMap.put("status", "退回");
				commitMap.put("status", "提交");
				for (Map<String, Object> temp : tempResult) {
					if (temp.get("status") == null) {
						continue;
					}
					if (templ.get("fregionCode").equals((temp.get("fregionCode").toString()))) {
						// if ("0".equals(temp.get("status").toString())) {
						//
						// tempMap.put(
						// "num",
						// Integer.parseInt(temp.get("num").toString())
						// + Integer.parseInt(tempMap.get(
						// "num").toString()));
						// }
						if ("1".equals(temp.get("status").toString())) {

							passMap.put("num", Integer.parseInt(temp.get("num").toString())
									+ Integer.parseInt(passMap.get("num").toString()));
						}
						// if ("-1".equals(temp.get("status").toString())) {
						//
						// notCommitMap
						// .put("num",
						// Integer.parseInt(temp.get("num")
						// .toString())
						// + Integer
						// .parseInt(notCommitMap
						// .get("num")
						// .toString()));
						// }
						if ("-2".equals(temp.get("status").toString())) {

							backMap.put("num", Integer.parseInt(temp.get("num").toString())
									+ Integer.parseInt(backMap.get("num").toString()));
						}

					}

				}

				commitMap.put("num", (int) backMap.get("num") + (int) passMap.get("num"));
				status.add(commitMap);
				if (backMap.get("status") != null) {
					status.add(backMap);
				}
				if (passMap.get("status") != null) {
					status.add(passMap);
				}
				// if (tempMap.get("status") != null) {
				// status.add(tempMap);
				// }
				// if (notCommitMap.get("status") != null) {
				// status.add(notCommitMap);
				// }
				map.put("data", status);
				ListResult.add(map);
			}
			result.add(ListResult);
			return result;
		}

		if (i == 2) {
			// 管辖区下乡镇行政编码
			Set<Map<String, Object>> area = new HashSet<Map<String, Object>>();
			// 解析乡镇
			for (Map map : tempResult) {
				Map<String, Object> temmap = new HashMap<String, Object>();
				temmap.put("fregionCode", (map.get("fregionCode")));
				temmap.put("fregionName", map.get("fregionName"));
				area.add(temmap);
			}

			// 统计乡镇下 所有 村社的审核状态
			for (Map<String, Object> templ : area) {
				Map<String, Object> map = new HashMap<String, Object>();
				// 状态和数量
				List<Map<String, Object>> status = new ArrayList<Map<String, Object>>();
				map.put("fregionName", templ.get("fregionName"));
				Map<String, Object> backMap = new HashMap<String, Object>();
				Map<String, Object> passMap = new HashMap<String, Object>();
				// Map<String, Object> tempMap = new HashMap<String, Object>();
				// Map<String, Object> notCommitMap = new HashMap<String,
				// Object>();
				Map<String, Object> commitMap = new HashMap<String, Object>();
				backMap.put("num", 0);
				passMap.put("num", 0);
				// tempMap.put("num", 0);
				// notCommitMap.put("num", 0);
				// passMap.put("status", "未处理");
				passMap.put("status", "通过");
				// notCommitMap.put("status", "未提交");
				backMap.put("status", "退回");
				commitMap.put("status", "提交");
				for (Map<String, Object> temp : tempResult) {
					if (temp.get("status") == null) {
						continue;
					}
					if (templ.get("fregionCode").equals((temp.get("fregionCode")))) {
						// if ("0".equals(temp.get("status").toString())) {
						//
						// tempMap.put(
						// "num",
						// Integer.parseInt(temp.get("num").toString())
						// + Integer.parseInt(tempMap.get(
						// "num").toString()));
						// }
						if ("1".equals(temp.get("status").toString())) {

							passMap.put("num", Integer.parseInt(temp.get("num").toString())
									+ Integer.parseInt(passMap.get("num").toString()));
						}
						// if ("-1".equals(temp.get("status").toString())) {
						//
						// notCommitMap
						// .put("num",
						// Integer.parseInt(temp.get("num")
						// .toString())
						// + Integer
						// .parseInt(notCommitMap
						// .get("num")
						// .toString()));
						// }
						if ("-2".equals(temp.get("status").toString())) {

							backMap.put("num", Integer.parseInt(temp.get("num").toString())
									+ Integer.parseInt(backMap.get("num").toString()));
						}

					}

				}

				commitMap.put("num", (int) backMap.get("num") + (int) passMap.get("num"));
				status.add(commitMap);
				if (backMap.get("status") != null) {
					status.add(backMap);
				}
				if (passMap.get("status") != null) {
					status.add(passMap);
				}
				// if (tempMap.get("status") != null) {
				// status.add(tempMap);
				// }
				// if (notCommitMap.get("status") != null) {
				// status.add(notCommitMap);
				// }
				map.put("data", status);
				ListResult.add(map);
			}
			result.add(ListResult);
			return result;
		}

		// 乡镇
		if (i == 3) {
			// 管辖区下乡镇行政编码
			Set<Map<String, Object>> area = new HashSet<Map<String, Object>>();
			// 解析乡镇
			for (Map map : tempResult) {
				Map<String, Object> temmap = new HashMap<String, Object>();
				temmap.put("fregionCode", (map.get("fregionCode").toString().substring(0, 12)));
				temmap.put("fregionName", map.get("fregionName"));
				area.add(temmap);
			}

			// 统计乡镇下 所有 村社的审核状态
			for (Map<String, Object> templ : area) {
				Map<String, Object> map = new HashMap<String, Object>();
				// 状态和数量
				List<Map<String, Object>> status = new ArrayList<Map<String, Object>>();
				map.put("fregionName", templ.get("fregionName"));
				Map<String, Object> backMap = new HashMap<String, Object>();
				Map<String, Object> passMap = new HashMap<String, Object>();
				// Map<String, Object> tempMap = new HashMap<String, Object>();
				// Map<String, Object> notCommitMap = new HashMap<String,
				// Object>();
				Map<String, Object> commitMap = new HashMap<String, Object>();
				backMap.put("num", 0);
				passMap.put("num", 0);
				// tempMap.put("num", 0);
				// notCommitMap.put("num", 0);
				// passMap.put("status", "未处理");
				passMap.put("status", "通过");
				// notCommitMap.put("status", "未提交");
				backMap.put("status", "退回");
				commitMap.put("status", "提交");
				for (Map<String, Object> temp : tempResult) {
					if (temp.get("status") == null) {
						continue;
					}
					if (templ.get("fregionCode").equals((temp.get("fregionCode").toString()).substring(0, 12))) {
						// if ("0".equals(temp.get("status").toString())) {
						//
						// tempMap.put(
						// "num",
						// Integer.parseInt(temp.get("num").toString())
						// + Integer.parseInt(tempMap.get(
						// "num").toString()));
						// }
						if ("1".equals(temp.get("status").toString())) {

							passMap.put("num", Integer.parseInt(temp.get("num").toString())
									+ Integer.parseInt(passMap.get("num").toString()));
						}
						// if ("-1".equals(temp.get("status").toString())) {
						//
						// notCommitMap
						// .put("num",
						// Integer.parseInt(temp.get("num")
						// .toString())
						// + Integer
						// .parseInt(notCommitMap
						// .get("num")
						// .toString()));
						// }
						if ("-2".equals(temp.get("status").toString())) {

							backMap.put("num", Integer.parseInt(temp.get("num").toString())
									+ Integer.parseInt(backMap.get("num").toString()));
						}

					}

				}

				commitMap.put("num", (int) backMap.get("num") + (int) passMap.get("num"));
				status.add(commitMap);
				if (backMap.get("status") != null) {
					status.add(backMap);
				}
				if (passMap.get("status") != null) {
					status.add(passMap);
				}
				// if (tempMap.get("status") != null) {
				// status.add(tempMap);
				// }
				// if (notCommitMap.get("status") != null) {
				// status.add(notCommitMap);
				// }
				map.put("data", status);
				ListResult.add(map);
			}
			result.add(ListResult);
			return result;
		}

		return null;
	}

	// solr的分页
	public static PageInfo getResultPage(List<Map<String, Object>> list, int pageNo, int pageSize, int fetchNum,
			int pageTotal, int total) {

		PageInfo page = new PageInfo();
		try {

			page.setCurrPageNo(pageNo);
			page.setPageSize(pageSize);
			page.setTotal(total);
			page.setPageTotal(pageTotal);
			page.setRows(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}

	public static void listSort(List<Map<String, Object>> resultList) {
		// resultList是需要排序的list，其内放的是Map
		// 返回的结果集
		Collections.sort(resultList, new Comparator<Map<String, Object>>() {

			public int compare(Map<String, Object> o1, Map<String, Object> o2) {

				Integer s1 = Integer.parseInt(o1.get("fUserType").toString());
				Integer s2 = Integer.parseInt(o2.get("fUserType").toString());

				if (s1 > s2) {
					return 1;
				} else {
					return -1;
				}
			}
		});

	}
	public static void listSortforNum(List<Map<String, Object>> resultList) {
		// resultList是需要排序的list，其内放的是Map
		// 返回的结果集
		Collections.sort(resultList, new Comparator<Map<String, Object>>() {
			@Override
			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
				
				Integer s1 = Integer.parseInt(o1.get("num").toString());
				Integer s2 = Integer.parseInt(o2.get("num").toString());
				
				if ((s1-s2) < 0) {
					return 1;
				} else if ((s1-s2) > 0){
					return -1;
				}else{
					return 0;
				}
			}
		});
		
	}
	public static void listSortforCount(List<Map<String, Object>> resultList) {
		// resultList是需要排序的list，其内放的是Map
		// 返回的结果集
		Collections.sort(resultList, new Comparator<Map<String, Object>>() {
			@Override
			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
				
				Integer s1 = Integer.parseInt(o1.get("count").toString());
				Integer s2 = Integer.parseInt(o2.get("count").toString());
				
				if ((s1-s2) < 0) {
					return 1;
				} else if ((s1-s2) > 0){
					return -1;
				}else{
					return 0;
				}
			}
		});
		
	}
	public static int getYear(){
		 Calendar cal = Calendar.getInstance();
		 int year = cal.get(Calendar.YEAR);
		return year;
	}
	public static boolean checkPassWd(String passWd) {
		String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z!@#$%^&*?=]{8,20}$";
		if (passWd.matches(regex)) {
			return true;
		}else {
			return false;
		}
	}
	
	public static int getEachMonthDay(int year,int month){
		if(year != 0 && month != 0){
			switch (month) {
			case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31;
			case 2: return isLeapYear(year)? 29:28;
			case 4: case 6:case 9:case 11: return 30;	
			default:break;
			}
		}
		return 1;
	}
	
	public static boolean isLeapYear(int year){
		if(year % 4 == 0 && year % 100!= 0 || year % 400 == 0){
			return true;
		}
		return false;
	}
	
	public static void listSortforNum1(List<Map<String, Object>> resultList) {
		// resultList是需要排序的list，其内放的是Map
		// 返回的结果集
		Collections.sort(resultList, new Comparator<Map<String, Object>>() {
			@Override
			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
				
				Double s1 = Double.parseDouble(o1.get("loginRate").toString());
				Double s2 = Double.parseDouble(o2.get("loginRate").toString());
				
				if ((s1-s2) < 0) {
					return 1;
				} else if ((s1-s2) > 0){
					return -1;
				}else{
					return 0;
				}
			}
		});
		
	}
	public static void listSortforNum2(List<Map<String, Object>> resultList) {
		// resultList是需要排序的list，其内放的是Map
		// 返回的结果集
		Collections.sort(resultList, new Comparator<Map<String, Object>>() {
			@Override
			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
				
				Double s1 = Double.parseDouble(o1.get("fillRate").toString());
				Double s2 = Double.parseDouble(o2.get("fillRate").toString());
				
				if ((s1-s2) < 0) {
					return 1;
				} else if ((s1-s2) > 0){
					return -1;
				}else{
					return 0;
				}
			}
		});
		
	}
	public static int getEachQuarterDay(int year,int quarter){
		switch (quarter) {
		case 1:return getEachMonthDay(year, 1)+getEachMonthDay(year, 2)+getEachMonthDay(year, 3);
		case 2:return getEachMonthDay(year, 4)+getEachMonthDay(year, 5)+getEachMonthDay(year, 6);
		case 3:return getEachMonthDay(year, 7)+getEachMonthDay(year, 8)+getEachMonthDay(year, 9);
		case 4:return getEachMonthDay(year, 10)+getEachMonthDay(year, 11)+getEachMonthDay(year, 12);
		default:
			break;
		}
		
		return 1;
	}
	
	public static void listSort3(List<Map<String, Object>> resultList) {
		// resultList是需要排序的list，其内放的是Map
		// 返回的结果集
		Collections.sort(resultList, new Comparator<Map<String, Object>>() {
			long t1 = 0l;
			long t2 = 0l;
			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
				try {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					 t1 = sdf.parse(o1.get("fTime").toString()).getTime();
					 t2 = sdf.parse(o2.get("fTime").toString()).getTime();
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (t1 > t2) {
					return -1;
				} else {
					return 1;
				}
			}
		});

	}
	public static boolean forceDelete(File file) {
        boolean result = file.delete();
        int tryCount = 0;
        while (!result && tryCount++ < 10) {
            System.gc();    //回收资源
            result = file.delete();
        }
        return result;
    }
	
	public static void listSortforExaminateRate(List<Map<String, Object>> resultList) {
		// resultList是需要排序的list，其内放的是Map
		// 返回的结果集
		Collections.sort(resultList, new Comparator<Map<String, Object>>() {
			@Override
			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
				
				Double s1 = Double.parseDouble(o1.get("fExaminateRate").toString());
				Double s2 = Double.parseDouble(o2.get("fExaminateRate").toString());
				
				if ((s1-s2) < 0) {
					return 1;
				} else if ((s1-s2) > 0){
					return -1;
				}else{
					return 0;
				}
			}
		});
		
	}
	
	public static void copy(File file, File toFile) throws Exception { 
	    byte[] b = new byte[1024]; 
	    int a=5; 
	    FileInputStream fis; 
	    FileOutputStream fos; 
	    if (file.isDirectory()) { 
	      String filepath = file.getAbsolutePath(); 
	      filepath=filepath.replaceAll("\\\\", "/"); 
	      String toFilepath = toFile.getAbsolutePath(); 
	      toFilepath=toFilepath.replaceAll("\\\\", "/"); 
	      int lastIndexOf = filepath.lastIndexOf("/"); 
	      toFilepath = toFilepath + filepath.substring(lastIndexOf ,filepath.length()); 
	      File copy=new File(toFilepath); 
	      //复制文件夹 
	      if (!copy.exists()) { 
	        copy.mkdir(); 
	      } 
	      //遍历文件夹 
	      for (File f : file.listFiles()) { 
	        copy(f, copy); 
	      } 
	    } else { 
	      if (toFile.isDirectory()) { 
	        String filepath = file.getAbsolutePath(); 
	        filepath=filepath.replaceAll("\\\\", "/"); 
	        String toFilepath = toFile.getAbsolutePath(); 
	        toFilepath=toFilepath.replaceAll("\\\\", "/"); 
	        int lastIndexOf = filepath.lastIndexOf("/"); 
	        toFilepath = toFilepath + filepath.substring(lastIndexOf ,filepath.length()); 
	          
	        //写文件 
	        File newFile = new File(toFilepath); 
	        fis = new FileInputStream(file); 
	        fos = new FileOutputStream(newFile); 
	        while ((a = fis.read(b)) != -1) { 
	          fos.write(b, 0, a); 
	        } 
	      } else { 
	        //写文件 
	        fis = new FileInputStream(file); 
	        fos = new FileOutputStream(toFile); 
	        while ((a = fis.read(b)) != -1) { 
	          fos.write(b, 0, a); 
	        } 
	      } 
	  
	    } 
	  } 
	
	public static String getBeforeYear(String year) {
		String year1 = year.substring(0, 4);
		String year2 = year.substring(4);
		year1 = (Integer.valueOf(year1) - 1) + "";
		year = year1 + year2;
		return year;
	}

	public static String getCurrTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date currTime = new Date();
		String curTime = formatter.format(currTime);
		return curTime;
	}

	public static String getEndTime(String year, String month) {
		String day = "";
		if (month.equals("01") || month.equals("03") || month.equals("05")
				|| month.equals("07") || month.equals("08")
				|| month.equals("10") || month.equals("12")) {
			day = "31";
		} else if (month.equals("02")) {
			if (isLeapYear(Integer.parseInt(year))) {
				day = "29";
			} else {
				day = "28";
			}
		} else {
			day = "30";
		}
		return year + "-" + month + "-" + day;
	}

	public static String dayTranslate(String day) {
		if (day.equals("01")) {
			day = "一号";
		}
		if (day.equals("02")) {
			day = "二号";
		}
		if (day.equals("03")) {
			day = "三号";
		}
		if (day.equals("04")) {
			day = "四号";
		}
		if (day.equals("05")) {
			day = "五号";
		}
		if (day.equals("06")) {
			day = "六号";
		}
		if (day.equals("07")) {
			day = "七号";
		}
		if (day.equals("08")) {
			day = "八号";
		}
		if (day.equals("09")) {
			day = "九号";
		}
		if (day.equals("10")) {
			day = "十号";
		}
		if (day.equals("11")) {
			day = "十一号";
		}
		if (day.equals("12")) {
			day = "十二号";
		}
		if (day.equals("13")) {
			day = "十三号";
		}
		if (day.equals("14")) {
			day = "十四号";
		}
		if (day.equals("15")) {
			day = "十五号";
		}
		if (day.equals("16")) {
			day = "十六号";
		}
		if (day.equals("17")) {
			day = "十七号";
		}
		if (day.equals("18")) {
			day = "十八号";
		}
		if (day.equals("19")) {
			day = "十九号";
		}
		if (day.equals("20")) {
			day = "二十号";
		}
		if (day.equals("21")) {
			day = "二十一号";
		}
		if (day.equals("22")) {
			day = "二十二号";
		}
		if (day.equals("23")) {
			day = "二十三号";
		}
		if (day.equals("24")) {
			day = "二十四号";
		}
		if (day.equals("25")) {
			day = "二十五号";
		}
		if (day.equals("26")) {
			day = "二十六号";
		}
		if (day.equals("27")) {
			day = "二十七号";
		}
		if (day.equals("28")) {
			day = "二十八号";
		}
		if (day.equals("29")) {
			day = "二十九号";
		}
		if (day.equals("30")) {
			day = "三十号";
		}
		if (day.equals("31")) {
			day = "三十一号";
		}

		return day;
	}
	public static List<String> dayDeal(String dayStart, String dayEnd) {
		List<String> list = new ArrayList<String>();
		String[] starts = dayStart.split("-");
		String[] ends = dayEnd.split("-");
		Calendar start = Calendar.getInstance();
		start.set(Integer.parseInt(starts[0]), Integer.parseInt(starts[1]) - 1,
				Integer.parseInt(starts[2]));
		Long startTIme = start.getTimeInMillis();

		Calendar end = Calendar.getInstance();
		end.set(Integer.parseInt(ends[0]), Integer.parseInt(ends[1]) - 1,
				Integer.parseInt(ends[2]));
		Long endTime = end.getTimeInMillis();

		Long oneDay = 1000 * 60 * 60 * 24l;

		Long time = startTIme;
		while (time <= endTime) {
			Date d = new Date(time);
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			// System.out.println(df.format(d));
			String day = df.format(d);
			list.add(day);
			time += oneDay;
		}
		return list;
	}

	public static List<String> quarterDeal(String quarterStart,
			String quarterEnd) {
		List<String> list = new ArrayList<String>();
		List<String> list2 = new ArrayList<String>();
		List<String> list3 = new ArrayList<String>();
		String[] quarterStarts = quarterStart.split("-");
		String[] quarterEnds = quarterEnd.split("-");
		if (!quarterStarts[0].equals(quarterEnds[0])) {
			for (int i = Integer.parseInt(quarterStarts[1]); i <= 4; i++) {
				list2.add(quarterStarts[0] + "第" + i + "季度");
			}
		} else {
			for (int i = Integer.parseInt(quarterStarts[1]); i <= Integer
					.parseInt(quarterEnds[1]); i++) {
				list2.add(quarterStarts[0] + "第" + i + "季度");
			}
		}
		if ((Integer.parseInt(quarterEnds[0]) - Integer
				.parseInt(quarterStarts[0])) >= 1) {

			for (int i = Integer.parseInt(quarterStarts[0]) + 1; i <= Integer
					.parseInt(quarterEnds[0]) - 1; i++) {
				for (int j = 1; j <= 4; j++) {
					list.add(i + "第" + j + "季度");
				}
			}

		}
		if (!quarterStarts[0].equals(quarterEnds[0])) {
			for (int i = 1; i <= Integer.parseInt(quarterEnds[1]); i++) {
				list3.add(quarterEnds[0] + "第" + i + "季度");
			}
		}
		list.addAll(list3);
		list2.addAll(list);
		return list2;
	}

	public static List<String> monthDeal(String monthStart, String monthEnd) {
		List<String> list = new ArrayList<String>();
		List<String> list2 = new ArrayList<String>();
		List<String> list3 = new ArrayList<String>();
		String[] monthStarts = monthStart.split("-");
		String[] monthEndEnds = monthEnd.split("-");
		if (!monthStarts[0].equals(monthEndEnds[0])) {
			for (int i = Integer.parseInt(monthStarts[1]); i <= 12; i++) {

				if (i < 10) {
					list2.add(monthStarts[0] + "-" + 0 + i);
				} else {
					list2.add(monthStarts[0] + "-" + i);
				}
			}
		} else {
			for (int i = Integer.parseInt(monthStarts[1]); i <= Integer
					.parseInt(monthEndEnds[1]); i++) {

				if (i < 10) {
					list2.add(monthStarts[0] + "-" + 0 + i);
				} else {
					list2.add(monthStarts[0] + "-" + i);
				}
			}
		}
		if ((Integer.parseInt(monthEndEnds[0]) - Integer
				.parseInt(monthStarts[0])) >= 1) {

			for (int i = Integer.parseInt(monthStarts[0]) + 1; i <= Integer
					.parseInt(monthEndEnds[0]) - 1; i++) {
				for (int j = 1; j <= 12; j++) {
					if (j < 10) {
						list.add(i + "-" + 0 + j);
					} else {
						list.add(i + "-" + j);
					}

				}
			}

		}
		if (!monthStarts[0].equals(monthEndEnds[0])) {
			for (int i = 1; i <= Integer.parseInt(monthEndEnds[1]); i++) {
				if (i < 10) {
					list3.add(monthEndEnds[0] + "-" + 0 + i);
				} else {
					list3.add(monthEndEnds[0] + "-" + i);
				}

			}
		}
		list.addAll(list3);
		list2.addAll(list);
		return list2;
	}
	public static int getSeason(Date date) {  
		  
        int season = 0;  
  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        int month = c.get(Calendar.MONTH);  
        switch (month) {  
        case Calendar.JANUARY:  
        case Calendar.FEBRUARY:  
        case Calendar.MARCH:  
            season = 1;  
            break;  
        case Calendar.APRIL:  
        case Calendar.MAY:  
        case Calendar.JUNE:  
            season = 2;  
            break;  
        case Calendar.JULY:  
        case Calendar.AUGUST:  
        case Calendar.SEPTEMBER:  
            season = 3;  
            break;  
        case Calendar.OCTOBER:  
        case Calendar.NOVEMBER:  
        case Calendar.DECEMBER:  
            season = 4;  
            break;  
        default:  
            break;  
        }  
        return season;  
    }
	
	 public static boolean deleteDir(File dir) {
	        if (dir.isDirectory()) {
	            String[] children = dir.list();
	          //递归删除目录中的子目录下
	            for (int i=0; i<children.length; i++) {
	                boolean success = deleteDir(new File(dir, children[i]));
	                if (!success) {
	                    return false;
	                }
	            }
	        }
	        // 目录此时为空，可以删除
	        return dir.delete();
	    }
	 
	 public static boolean deleteDirNotIsEmpty(File dir) {
		 if (dir.isDirectory()) {
			 String[] children = dir.list();
			 //递归删除目录中的子目录下
			 for (int i=0; i<children.length; i++) {
				 boolean success = deleteDir(new File(dir, children[i]));
				 if (!success) {
					 return false;
				 }
			 }
		 }
		 // 目录此时为空，可以删除
//		 return dir.delete();
		 return false;
	 }
	 
	 /**
	  * 组合sql中的时间
	  * @author slm
	  * @param fYear
	  * @param fQuarter
	  * @param fMonth
	  * @param fDay
	  * @param fStartTime
	  * @param fEndTime
	  * @date 2017年12月29日 10点43分
	  * @return
	  */
	 public static Map<String,Object> getMapTime(String fYear,String fQuarter,String fMonth,String fDay,String fStartTime,String fEndTime,int way) {
		 Map<String,Object> map = new HashMap<>();
		 SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd" );
		 int days = 0;
		 String start = "fStartTime";
		 String end = "fEndTime";
		 if(way == 0 || way == 1) {
			 if(way == 1) {
				 start = "startTime";
				 end = "endTime";
			 }
			 
			 if(!Util.isNullString(fStartTime) && Util.isNullString(fEndTime)) {
				 map.put(start,   fStartTime+" 00:00:00");
				 map.put(end, sdf.format(new Date()) +" 23:59:59");
				 
			 }else if(Util.isNullString(fStartTime) && !Util.isNullString(fEndTime)) {
				 map.put(start,   "2000-01-01 00:00:00");
				 map.put(end, fEndTime + " 23:59:59");
				 
			 }else if(!Util.isNullString(fStartTime) && !Util.isNullString(fEndTime)) {
				 map.put(start,   fStartTime + " 00:00:00");
				 map.put(end, fEndTime + " 23:59:59");
			 }
			 
			 if(!Util.isNullString(fYear)){
				 map.put(start, fYear + "-01-01 00:00:00");
				 map.put(end, fYear + "-12-31 23:59:59");
					if(!Util.isNullString(fQuarter)){
					int qTime= new Integer(fQuarter);
					switch (qTime) {
					case 1:
						 map.put(start, fYear + "-01-01 00:00:00");
						 map.put(end, fYear + "-03-31 23:59:59");
						break;
					case 2:
						map.put(start, fYear + "-04-01 00:00:00");
						map.put(end,  fYear + "-06-30 23:59:59");
						break;
					case 3:
						map.put(start, fYear + "-07-01 00:00:00");
						map.put(end,  fYear + "-09-30 23:59:59");
						break;
					case 4:
						map.put(start, fYear + "-10-01 00:00:00");
						map.put(end,  fYear + "-12-31 23:59:59");
						break;

					default:
						break;
					}
						if(!Util.isNullString(fMonth)){
							days = Util.getEachMonthDay(new Integer(fYear), new Integer(fMonth));
							if(Util.isNullString(fDay)){
								map.put(start, fYear + "-" + fMonth + "-01 00:00:00");
								map.put(end,   fYear + "-" + fMonth + "-"+days+" 23:59:59");
							}else {
								map.put(start,   fYear + "-" + fMonth + "-"+fDay + " 00:00:00");
								map.put(end,     fYear + "-" + fMonth + "-"+fDay + " 23:59:59");
							}
						}
					}
				}
			 
			 
		 }else {
			 
			 if(!Util.isNullString(fStartTime) && Util.isNullString(fEndTime)) {
				 map.put(start,   fStartTime + "T00:00:00Z");
				 map.put(end, sdf.format(new Date())+"T23:59:59Z");
				 
			 }else if(Util.isNullString(fStartTime) && !Util.isNullString(fEndTime)) {
				 map.put(start,   "2000-01-01"+ "T00:00:00Z");
				 map.put(end, fEndTime+"T23:59:59Z");
				 
			 }else if(!Util.isNullString(fStartTime) && !Util.isNullString(fEndTime)) {
				 map.put(start,   fStartTime+ "T00:00:00Z");
				 map.put(end, fEndTime+ "T23:59:59Z");
			 }
			 
			 if(!Util.isNullString(fYear)){
				 map.put(start, fYear + "-01-01T00:00:00Z");
				 map.put(end, fYear + "-12-31T23:59:59Z");
					if(!Util.isNullString(fQuarter)){
					int qTime= new Integer(fQuarter);
					switch (qTime) {
					case 1:
						 map.put(start, fYear + "-01-01T00:00:00Z");
						 map.put(end, fYear + "-03-31T23:59:59Z");
						break;
					case 2:
						map.put(start, fYear + "-04-01T00:00:00Z");
						map.put(end,  fYear + "-06-30T23:59:59Z");
						break;
					case 3:
						map.put(start, fYear + "-07-01T00:00:00Z");
						map.put(end,  fYear + "-09-30T23:59:59Z");
						break;
					case 4:
						map.put(start, fYear + "-10-01T00:00:00Z");
						map.put(end,  fYear + "-12-31T23:59:59Z");
						break;

					default:
						break;
					}
					if(!Util.isNullString(fMonth)){
						days = Util.getEachMonthDay(new Integer(fYear), new Integer(fMonth));
						if(Util.isNullString(fDay)){
							map.put(start, fYear + "-" + fMonth + "-01T00:00:00Z");
							map.put(end,   fYear + "-" + fMonth + "-"+days+"T23:59:59Z");
						}else {
							map.put(start,   fYear + "-" + fMonth + "-"+fDay + "T00:00:00Z");
							map.put(end,     fYear + "-" + fMonth + "-"+fDay + "T23:59:59Z");
						}
					}
				}
			}
		 }
		
		 return map;
	 }
	 
	
}